package com.ming.javaweb.util;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static String convertTimestamp2Str(Timestamp timestamp){
        Instant instant = new Date(timestamp.getTime()).toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return formatter.format(localDateTime);
    }

    /**
     *
     * @param str
     * @return
     */
    public static Date convertStr2Date(String str){
        LocalDateTime parse = LocalDateTime.parse(str, formatter);
        Instant instant = parse.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static String convertDate2Str(Date birthDay){
        Instant instant = birthDay.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return formatter.format(localDateTime);
    }
}
